#!/bin/bash
#
# Exports public security keys
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 19 November 2013
#
#   Copyright 2012-2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## p_cmd_export_key_help
##
## Echoes help information for the export_key command. Returns 0.
##
function p_cmd_export_key_help() {
	cat << EOF
Usage: pulley export_key [options] <output file>

Exports a public signature verification key to the path specified in
<output file>. If no [options] are specified, exports the default public
key for the configured subscription using the configured security provider.

Options are:

--keyid <id>
  Selects the public key with the specified <id>. If this option is
  omitted, the default key will be selected.

--security <provider>
  Obtain the key from the specified security <provider>. Without this
  option, the security provider specified by the subscription will be used.

--section <section>
  Use the specified <section> of the Pulley key store. If this option is not
  specified, the subscription section is used as the default.

EOF
	return 0
}


## p_cmd_export_key_summary
##
## Echoes summary information for the export_key command. Returns 0.
##
function p_cmd_export_key_summary() {
	echo "Exports a public security key to a file"
	return 0
}


## p_cmd_export_key [options] <output file>
##
## Implements the Pulley export_key command, exporting a public key to the
## specified <output file>.
##
## [options] are:
##   --keyid <id>             Select public key with specified <id>
##   --security <provider>    Set security <provider> manually
##   --section <section>      Export from the given <section> of the key store
##
## Returns 0 on success, non-zero if an error occurs.
##
function p_cmd_export_key() {
	local keyid=
	local provider=$(psub_get_provider)
	local section="subscription"
	
	local lopts="keyid:,security:,section:"
	local args=$(getopt -o "+" -l "${lopts}" -n "$0" -- "$@")
	local status=$?
	
	if ((status == 0)); then
		eval set -- "${args}"
		
		while [[ "$1" != "--" ]]; do
			case "$1" in
				--keyid)
					shift
					keyid="$1"
					shift
				;;
				--security)
					shift
					provider="$1"
					shift
				;;
				--section)
					shift
					section="$1"
					shift
				;;
			esac
		done
		
		shift
	
		if [[ $# -ne 1 ]]; then
			echo "Usage: pulley export_key [options] <output file>"
			status=2
		fi
	fi
	
	if ((status == 0)); then
		psec_export_key "${section}" "${provider}" "${outfile}" ${keyid}
		status=$?
	fi
	
	return ${status}
}
